package com.example.chapter04;

import java.util.Random;

/**
 * 按位操作符
 * @date 2024/4/29
 */
public class BitManipulation {
    public static void main(String[] args) {
        Random rand = new Random(47);
        int i = rand.nextInt();
        int j = rand.nextInt();
        printBinarylnt("-1", -1);
        printBinarylnt("+1", +1);
        int maxpos = 2147483647;
        printBinarylnt("maxpos", maxpos);
        int maxneg = -2147483648;
        printBinarylnt("maxneg", maxneg);
        printBinarylnt("i", i);
        printBinarylnt("i", ~i);
        printBinarylnt("-i", -i);
        printBinarylnt("j", j);
        printBinarylnt("i & j", i & j);
        printBinarylnt("i | j", i | j);
        printBinarylnt("i ^ j", i ^ j);
        printBinarylnt("i << 5", i << 5);
        printBinarylnt("i >> 5", i >> 5);
        printBinarylnt("(~i) >> 5", (~1) >> 5);
        printBinarylnt("i >>> 5", i >>> 5);
        printBinarylnt("(~1) >>> 5", (~1) >>> 5);

        long I = rand.nextLong();
        long m = rand.nextLong();
        printBinaryLong("-1L", -1L);

        printBinaryLong("+1L", +1L);
        long ll = 9223372036854775807L;
        printBinaryLong("maxpos", ll);
        long lln = -9223372036854775808L;
        printBinaryLong("maxneg", lln);
        printBinaryLong("I", I);
        printBinaryLong("~l", ~ll);
        printBinaryLong("-I", -I);
        printBinaryLong("m", m);
        printBinaryLong("l & m", I & m);
        printBinaryLong("I | m", I | m);
        printBinaryLong("I ^ m", 1 ^ m);
        printBinaryLong("I << 5", 1 << 5);
        printBinaryLong("l >> 5", I >> 5);
        printBinaryLong("(~I) >> 5", (~ll) >> 5);
        printBinaryLong("I >>> 5", I >>> 5);
        printBinaryLong(" (~l) >>> 5", (~ll) >>> 5);

    }

    static void printBinarylnt(String s, int i) {
        System.out.println(s + ", int: " + i + ", binary: \n   " + Integer.toBinaryString(i));
    }

    static void printBinaryLong(String s, long I) {
        System.out.println(s + ", long: " + I + ", binary:\n  " + Long.toBinaryString(I));
    }
}
